package com.tina.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tina.entity.TiRole;
import com.tina.entity.TiUsersRoleRef;
import com.tina.entity.vo.TiMenuVo;
import com.tina.model.ResultFormat;
import com.tina.model.ResultFormatPaging;
import com.tina.service.TiRoleService;
import com.tina.util.RedisUtil;
import com.tina.util.ResultPagingUtil;
import com.tina.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/tiRole")
public class TiRoleController {
    @Autowired
    private TiRoleService tiRoleService;

    @Autowired
    private RedisUtil redisUtil;

    private Logger logger = LoggerFactory.getLogger(TiRoleController.class);

    /**
     * 查询角色信息
     *
     * @return
     */
    @GetMapping("queryRoleInfo")
    @ApiOperation(value = "用户信息")
    public ResultFormat queryRoleInfo() {

        AbstractWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_state", 0);
        List<TiRole> sqlList = tiRoleService.list(wrapper);
        if (sqlList.size() > 0 && sqlList != null) {

            redisUtil.del("tiRoleId");

            for (TiRole tiRole : sqlList) {
                redisUtil.del("tiRole:" + tiRole.getRoleId());
                redisUtil.set("tiRole:" + tiRole.getRoleId(), tiRole);
                redisUtil.lSet("tiRoleId", tiRole.getRoleId());
            }
            return ResultUtil.success(sqlList);
        }

        return ResultUtil.success(100, "暂无数据..");
    }

    /**
     * 通过id查询用户角色信息
     *
     * @param userId
     * @return
     */
    @PostMapping("queryUserRoleInfo")
    @ApiOperation(value = "通过id查询用户角色信息")
    public ResultFormat queryUserRoleInfo(Integer userId) {
        logger.info("userId={}", userId);
        TiUsersRoleRef tiUsersRoleRef = tiRoleService.queryUserRoleInfo(userId);
        if (tiUsersRoleRef != null) {
            return ResultUtil.success(tiUsersRoleRef);
        }
        return ResultUtil.success(100, "暂无数据");
    }

    /**
     * 修改 角色 状态
     *
     * @param roleState
     * @param roleId
     * @return
     */
    @PutMapping("updateRoleState")
    @ApiOperation(value = "修改角色状态")
    public ResultFormat updateRoleState(Integer roleState, Integer roleId) {
        logger.info("roleState,roleId={}", roleState + " -- " + roleId);

        if (roleState != null && roleId != null) {
            TiRole tiRole = new TiRole();
            tiRole.setRoleId(roleId);
            tiRole.setRoleState(roleState);
            Long delete = redisUtil.delete("tiRole:" + roleId);
            logger.info("delete={}", delete);

            boolean update = tiRoleService.updateById(tiRole);
            logger.info("update={}", update);
            return ResultUtil.success(update);
        }

        return ResultUtil.error(100, "修改失败");
    }

    /**
     * 条件查询角色信息并分页
     *
     * @param tiRole
     * @return
     */
    @GetMapping("queryRoleList")
    @ApiOperation(value = "条件查询角色信息并分页")
    public ResultFormatPaging queryRoleList(TiRole tiRole, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        IPage<TiRole> tiRoleIPage = tiRoleService.queryRoleList(tiRole, page, limit);
        List<TiRole> records = tiRoleIPage.getRecords();
        logger.info("recordsTiRole={}", records);
        return ResultPagingUtil.pagingSuccess(0, tiRoleIPage.getTotal(), records);
    }

    /**
     * 添加角色
     *
     * @return
     */
    @PostMapping("saveRole")
    @ApiOperation(value = "添加角色")
    public ResultFormat saveRole(TiRole tiRole, String select) throws Exception {
        Integer saveRole = tiRoleService.saveRole(tiRole, select);
        return ResultUtil.success(saveRole, "");
    }

    /**
     * 查询角色
     *
     * @return
     */
    @PostMapping("queryRoleInfo")
    @ApiOperation(value = "添加角色")
    public ResultFormat queryRoleInfo(TiRole tiRole) throws Exception {
        TiRole queryRoleInfo = tiRoleService.queryRoleInfo(tiRole);
        if (queryRoleInfo != null) {
            return ResultUtil.success(queryRoleInfo);
        }
        return ResultUtil.success(100, "暂无数据");
    }


    /**
     * 修改角色信息
     *
     * @return
     */
    @PutMapping("editRole")
    @ApiOperation(value = "修改角色信息")
    public ResultFormat editRole(TiRole tiRole, String select) {
        logger.info("select={}", select);

        Integer editRole = tiRoleService.editRole(tiRole, select);

        return ResultUtil.success(editRole, "");
    }

    /**
     * 通过角色id查询菜单权限
     *
     * @param roleId
     * @tiPermission
     */
    @GetMapping("queryRoleMenuRefListByRoleId")
    @ApiOperation(value = "查询角色菜单关联数据")
    public ResultFormat queryRoleMenuRefList(Long roleId) {
        logger.info("roleId={}", roleId);
        List<TiMenuVo> queryRoleMenuRefListByRoleId = tiRoleService.queryRoleMenuRefListByRoleId(roleId);
        if (queryRoleMenuRefListByRoleId.size() > 0 && queryRoleMenuRefListByRoleId != null) {
            return ResultUtil.success(200, "查询成功", queryRoleMenuRefListByRoleId);
        }
        return ResultUtil.success(100, "操作失败");
    }
}
